﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dictionarymaker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string ebook = "";
        List<string> dictionary = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ebook = System.IO.File.ReadAllText(ofd.FileName);
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            foreach (string word in ebook.Split(' '))
            {
                string blargh = word.Replace("\n\r", "\n").Replace("\r", "").Replace(";", "").Replace("\"", "").Replace("'", "").Replace(".", "").Replace("-", "").Replace(")", "").Replace("(", "").Replace("/", "").Replace("\\", "").Replace(":", "").Replace("#", "").Replace("[", "").Replace("]", "").Replace("*", "").Replace(",", "").Replace("\n\n", "").Trim();
                if (!dictionary.Contains(blargh))
                {
                    dictionary.Add(blargh);
                }
            }
            MessageBox.Show("OK! Read file with " + dictionary.Count.ToString() + " Words!\nPress Save Dictionary to save the dictionary to a newline seperated file.");
            label1.Text = "Amount of words in dictionary: " + dictionary.Count.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                StringBuilder sbuilder = new StringBuilder();
                for (int i = 0; i < dictionary.Count; i++)
                {
                    sbuilder.Append(dictionary[i] + "\n");
                }
                System.IO.File.WriteAllText(sfd.FileName,sbuilder.ToString());
            }
        }
    }
}
